package com.kedou.sdk.secret;

import android.text.TextUtils;

import com.kedou.sdk.utils.WL;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by Administrator on 2017/5/27 0027.
 */

public final class MD5 {

    private static final String TAG = MD5.class.getSimpleName();

    private MD5() {
    }

    public static String encrypt(String str) {
        if (!TextUtils.isEmpty(str)) {
            try {
                MessageDigest md = MessageDigest.getInstance("MD5");
                byte[] buffer = md.digest(str.getBytes("UTF-8"));

                StringBuffer md5buff = new StringBuffer();
                int digital;
                for (int i = 0; i < buffer.length; i++) {
                    digital = buffer[i];
                    if (digital < 0) {
                        digital += 256;
                    }
                    if (digital < 16) {
                        md5buff.append("0");
                    }
                    md5buff.append(Integer.toHexString(digital));
                }
                return md5buff.toString().toUpperCase();
            } catch (NoSuchAlgorithmException ex) {
                WL.e(TAG, "NoSuchAlgorithmException");
            } catch (UnsupportedEncodingException ex) {
                WL.e(TAG, "UnsupportedEncodingException");
            }
        }
        return "";
    }
}
